/**
 * Created by zhangyi on 2017/11/8.
 */
mainApp.factory("sumweekFactory",function(){
    var service={};

    service.getListData=function(currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 8
        }
        var list = "";
        RequestUtils.postJson('../week1/get',obj).done(function (resp) {
            if(resp.resultCode==0){
                console.log(resp.data);
                list=resp.data;
                CommonUtils.buildFailHint("列表获取","成功");
            }else{
                CommonUtils.buildFailHint('列表获取', '失败');
            }
        });

        return list;
    };

    //新增周清记录service
    service.addSumweekData=function(sumweekObj){
        RequestUtils.postForm('../week1/add',sumweekObj).done(function (resp) {
            if(resp.resultCode==0){
                CommonUtils.buildFailHint("列表获取","成功");
                location.reload(true);
            }else{
                CommonUtils.buildFailHint('列表获取', '失败');
            }
        });
    };

    //据id查询service
    service.getSumweekById=function(id){
        var list="";
        RequestUtils.getJson('../week1/get/'+id).done(function (resp) {
            console.log(resp);
            if(resp.resultCode==0){
                list=resp.data;
                CommonUtils.buildFailHint("数据获取","成功");
            }else{
                CommonUtils.buildFailHint('数据获取', '失败');
            }
        });
        return list;
    };

    //修改周清记录
    service.Update=function(sumweekObj){
        RequestUtils.postForm('../week1/update',sumweekObj).done(function (resp) {
            console.log(resp);
            if(resp.resultCode==0){
                CommonUtils.buildFailHint("列表修改","成功");
                location.reload(true);
            }else{
                CommonUtils.buildFailHint('列表修改', '失败');
            }
        });
    };

    //按工号和时间区间查询
    service.selectSumweekByJobnumDate=function(sumweekWhereObj){
        var list="";
        RequestUtils.postJson('../week1/getByEidTime/'+sumweekWhereObj.jobnum+"/"+sumweekWhereObj.startDate+"/"+sumweekWhereObj.endDate,obj)
            .done(function (resp) {
            if(resp.resultCode==0){
                list=resp.data;
                CommonUtils.buildFailHint("列表获取","成功");
                location.reload(true);
            }else{
                CommonUtils.buildFailHint('列表获取', '失败');
            }
        });
        return list;
    };

    //删除
    service.deleteSumweek=function(id){
        RequestUtils.getJson('../week1/delete/'+id).done(function (resp) {
            console.log(resp);
            if(resp.resultCode==0){
                console.log(resp);
                CommonUtils.buildFailHint("数据删除","成功");
                location.reload(true);
            }else{
                CommonUtils.buildFailHint('数据删除', '失败');
            }
        });
    };

    return service;
});

mainApp.controller("sumweekController",function($scope,$http,sumweekFactory){
    $scope.title="周清管理";

    $scope.sumweekList=sumweekFactory.getListData(1);
    $scope.list=$scope.sumweekList.items;

    //打开新增模态框
    $scope.openAddSumweekModal=function(){
        $("#addSumweekModal").modal("show");
    };

    //新增
    $scope.addSumweek=function(sumweekObj){
        var sumweek={
            "key_tasks":sumweekObj.key_tasks,
            "create_start_time":sumweekObj.create_start_time,
            "plan_finish_time":sumweekObj.plan_finish_time,
            "actual_state":sumweekObj.actual_state,
            "actual_finish_time":sumweekObj.actual_finish_time,
            "week_update_time":sumweekObj.week_update_time,
            "employee.id":1//固定责任人
        };
        console.log(sumweek);
        sumweekFactory.addSumweekData(sumweek);

        $("#addSumweekModal").modal("hide");
        $scope.$apply();
    };

    //打开修改模态框，据id查询
    $scope.selectSumweek=function(id){
        var sumweekData=sumweekFactory.getSumweekById(id);
        $scope.updateSumweek=sumweekData;
        $("#updateSumweekModal").modal("show");
    };

    //修改
    $scope.Update=function(updateSumweek){
        var sumweek={
            "id":updateSumweek.id,
            "key_tasks":updateSumweek.key_tasks,
            "create_start_time":updateSumweek.create_start_time,
            "plan_finish_time":updateSumweek.plan_finish_time,
            "actual_state":updateSumweek.actual_state,
            "actual_finish_time":updateSumweek.actual_finish_time,
            "week_update_time":updateSumweek.week_update_time,
            "employee.id":1//固定责任人
        };
        console.log(updateSumweek)
        sumweekFactory.Update(sumweek);

        console.log(updateSumweek);
        sumweekFactory.Update(updateSumweek);

        $("#updateSumweekModal").modal("hide");
        //$scope.$apply();
    };

    //据工号和时间区间查询
    $scope.sumweekWhere;
    $scope.selectWhere=function(sumweekWhereObj){
        $scope.sumweekList=sumweekFactory.selectSumweekByJobnumDate(sumweekWhereObj);
        $scope.list=$scope.sumweekList.items;
    };

    //删除
    $scope.delete=function(id){
        console.log(id);
        sumweekFactory.deleteSumweek(id);
    };

    //翻页
    $scope.NextListData=function(pageIndex){
        $scope.sumweekList=sumweekFactory.getListData(pageIndex);
        $scope.list=$scope.sumweekList.items;

    }

    CommonUtils.setDatetimepicker("oneDatetimepicker");
    CommonUtils.setDatetimepicker("twoDatetimepicker");
    CommonUtils.setDatetimepicker("threeDatetimepicker");
    CommonUtils.setDatetimepicker("fourDatetimepicker");
    CommonUtils.setDatetimepicker("fiveDatetimepicker");
    CommonUtils.setDatetimepicker("sixDatetimepicker");
    CommonUtils.setDatetimepicker("sevenDatetimepicker");
    CommonUtils.setDatetimepicker("eightDatetimepicker");
    CommonUtils.setDatetimepicker("startDatetimepicker");
    CommonUtils.setDatetimepicker("endDatetimepicker");
});